package com.isp.org.server.domain.service;

import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isp.common.model.ResultCode;
import com.isp.common.model.exception.APIRuntimeException;
import com.isp.org.api.dto.user.UserVO;
import com.isp.org.server.domain.entity.UserEntity;
import com.isp.org.server.domain.mapper.UserMapper;
import com.isp.org.server.interfaces.model.ResultCodeEnum;
import com.isp.org.server.interfaces.model.user.mapper.UserMapStruct;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author linsy
 * @version 1.0
 * @created 2024/3/13 14:25
 */
@Service
@RequiredArgsConstructor
public class UserService extends ServiceImpl<UserMapper, UserEntity> {

    @Cached(name = "user::", key = "#uniqueId", cacheType = CacheType.BOTH)
    public UserEntity getByUniqueId(String uniqueId) {
        UserEntity user = this.getOne(new QueryWrapper<UserEntity>()
                .eq("username", uniqueId)
                .or()
                .eq("mobile", uniqueId));
        if (Objects.isNull(user)) {
            throw new APIRuntimeException(ResultCodeEnum.USER_QUERY_NOT_FOUND_BY_UNION_ID);
        }
        return user;
    }

}
